steps360=400.0
degperstep=360.0 / steps360
TIMERPERIOD=1.0e-7
tregmin=65535
totaltime=2.0
#vi = degperstep / (TIMERPERIOD * tregmin)
vi=180.0
vf=1440.0
accel=360.0 
ttotal=(vf-vi)/accel

set macros
NOYTICS = "set format y ''; unset ylabel"
YTICS = "set format y '%1.1f'; set ylabel ''"
YNTICS = "set format y '%g'; set ylabel ''"

# Linear form
lvnorm(t) = t

# Sigmoid form
ef=10 # shape of the sigmoid, larger ef makes it sharper
svnorm(t)=1 / (1 + exp(-(ef*t - (ef/2))))



set terminal wxt font "/usr/share/fonts/dejavu/DejaVuSans.ttf" 8 size 640,960
#set terminal png font "/usr/share/fonts/dejavu/DejaVuSans.ttf" 8 size 640,960
#set output "figs/speedlinear.png"

set multiplot layout 4, 2 rowsfirst

@YTICS
set key outside bottom center title 'Normalized: v(t) = t'
plot [t=0:1] lvnorm(t) title ''
@NOYTICS
set key outside bottom center title 'Acceleration: a(t)=1'
plot [t=0:1] 1.0 title ''

#set output "speeds.png"
#vi=1440
#vf=degperstep / (TIMERPERIOD * tregmin)
lv(t)=vi + ((vf-vi)*(t/totaltime))
lvf(t)=vi + (accel*t)

lvd(t)=vf + ((vi-vf)*(1 - t/totaltime))
sv(t) = vi + (vf - vi) * (1 / (1+exp(-(((ef/totaltime)*t-(ef/2))))))
svd(t) = vf + (vi - vf) * (1 / (1+exp(-((ef - ((ef/totaltime)*t)) - (ef/2)))))
@YTICS
set key outside bottom center title 'Fixed total time 2s, resulting a=770°/s'
plot [t=0:totaltime] lv(t) title ''
@NOYTICS
set key outside bottom center title 'Fixed accel. rate a=360°/s, time=3.5s'
plot [t=0:ttotal] lvf(t) title ''

# Steps
ls(t)=floor((steps360/360) * (vi*t + ((vf-vi)*((t*t)/(2*totaltime)))))
lsf(t)=floor((steps360/360) * (vi*t + ((vf-vi)*((t*t)/2))))
@YTICS
set key outside bottom center title 'Fixed total time: step number'
plot [t=0:totaltime] ls(t) title ''
@YTICS
set key outside bottom center title 'Fixed accel. rate: step number'
plot [t=0:ttotal] lsf(t) title ''


ss(t)=vi*t + (vf-vi)*(totaltime/ef)*log(1+exp((((ef/totaltime)*t-(ef/2)))))

# Period in timer unit
#lp(t)=(degperstep*1e7) / lv(t) 
lp(t)=(degperstep) / lv(t) 
#lpf(t)=(degperstep*1e7) / lvf(t)
lpf(t)=(degperstep) / lvf(t)
@YNTICS
set key outside bottom center title 'Fixed total time: step period'
plot [t=0:totaltime] lp(t) title ''
@YNTICS
set key outside bottom center title 'Fixed accel. rate: step period'
plot [t=0:ttotal] lpf(t) title ''

sp(t)=(degperstep*1e7) / sv(t) 
#plot [t=0:totaltime] lv(t) , floor(ls(t)) with histeps
#plot [t=0:totaltime]  lp(t)
#plot [t=0:totaltime] sv(t) , floor(ss(t)) with histeps
#plot [t=0:totaltime]  sp(t)
#set parametric
#plot [t=0:totaltime] floor(ls(t)),lv(t)
#plot [t=0:totaltime] floor(ls(t)),lp(t)
#plot [t=0:totaltime] floor(ss(t)),sv(t)
#plot [t=0:totaltime] floor(ss(t)),sp(t), floor(ls(t)), lp(t)

unset multiplot

unset macros